var modelAddModule=angular.module('sysUserAddApp', []);
    modelAddModule.controller('sysUserAddCtr',['$scope', '$http',function($scope, $http) {
		// 模型名称校验默认不显示
		$scope.checkUserAccount = false;
		$scope.getOrganizaAdd = function(){
			$('#organizaAdd').modal({remote: basePath+'/basicData/department/department_selList.jsp'})
		};
		// 点击保存按钮，新增一条数据
		$scope.saveUser = function() {
			// form表单的验证 数据不对，return false
			var formValid = $scope.userForm.$valid;
				if (!$scope.userAccount) {
					toastr.warning("用户账号不可为空！");
					formValid = false;
					return false;
				}else if (!$scope.userName) {
					toastr.warning("用户名称不可为空！");
					formValid = false;
					return false;
				}else if (!$scope.userEmail) {
					toastr.warning("用户邮箱不可为空！");
					formValid = false;
					return false;
				}else if (!$scope.userPhone) {
					toastr.warning("用户电话不可为空！");
					formValid = false;
					return false;
				}/*else if (!$scope.userTelephone) {
					toastr.warning("手机号不可为空！");
					formValid = false;
					return false;
				}*/
				else if ($("#upname").val() == '') {
					toastr.warning("部门名称不可为空！");
					formValid = false;
					return false;
				}
			if (formValid) { // 表单通过验证后提交
				checkUserAccount(); // 校验属性名称是否重复
			}
		};

		function checkUserAccount() {
			var result = true;
			$http({
				url : basePath+'/sysUser/sameUserAccountValidate',
				method : 'POST',
				params : {
					'userAccount' : $scope.userAccount
				}
			}).success(function(data, header, config, status) {
				if(data.success!=false){
					submitData();
				}else{
					$scope.checkUserAccount = true;
					return;
				}
			}).error(function(data, header, config, status) {
				// 处理响应失败
				result = false;
			});
		}
		// form表单提交方法 submit(若是校验都通过啦，数据便可以提交啦)
		function submitData() {
			$scope.organizationId=angular.element($("#organizationId")).val();
			//alert($scope.organizationId);
			$http({
				url : basePath+'/sysUser/addUser',
				method : 'POST',
				params : {
					'userAccount'  : $scope.userAccount,// 模型名称
					'userName'  : $scope.userName,// 模型描述
					'userPassword'  : $scope.userPassword,// 模型类型
					'userEmail' : $scope.userEmail,
					'userPhone'   :  $scope.userPhone,
					'userTelephone' : $scope.userTelephone,
					'organizationId' : $("#upcode").val(),
					'userRemark' : $scope.userRemark
				}
			}).success(function(data, header, config, status) {
				toastr.success("操作成功");
				setTimeout(function() {
					location.href = basePath+'/system/sysUser/user_apply.jsp';
				}, 500);
			}).error(function(data, header, config, status) {
				toastr.error("调用错误！");
				setTimeout(function() {
					location.href = basePath+'/system/sysUser/user_apply.jsp';
				}, 500);
			});
		}
		//  点击取消按钮，跳转到查询页面
		$scope.backUser = function() {
			location.href = basePath+'/system/sysUser/user_apply.jsp';
		};
}]);